<?php

class Bet_Model_Mapper {

	// fields
	private $_id;

	protected $_dbTableName;
	protected $_dbTable;

	// getters and setters

	/**
	 *
	 * @var string $id
	 * @return void
	 */
	public function setId($id) {
		$this->_id = $id;
	}

	/**
	 * @return string
	 */
	public function getId() {
		return $this->_id;
	}

	public function setDbTableName($_dbTableName) {
		$this->_dbTableName = $_dbTableName;
	}

	public function getDbTableName() {
		return $this->_dbTableName;
	}

	/**
	 * Set table data gateway
	 * @param Zend_Db_Table_Abstract $dbTable
	 * @return Bet_Model_Mapper
	 */
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	/**
	 * @return Zend_Db_Table_Abstract
	 */
	public function getDbTable() {
		if (null === $this->_dbTable) {
			if (empty($this->_dbTableName)) {
				throw new Exception("Table data gateway's name must be specified");
			}
			$this->setDbTable($this->_dbTableName);
		}
		return $this->_dbTable;
	}

	/**
	 * Save or update an instance's data
	 * @var array $data
	 * @return boolean Saving result - TRUE if successed, otherwise FALSE
	 */
	public function save($data) {
		$id = $data[$this->_id];
		if (!isset($id)) {
			$data["created_date"] = date('Y-m-d H:i:s');
			return $this->getDbTable()->insert($data);
		} else {
			$data["updated_date"] = date('Y-m-d H:i:s');
			return $this->getDbTable()->update($data, array($this->_id . ' = ?' => $id));
		}
	}

	/**
     * Fetches rows by primary key. This utilizes the find method
     * of Zend_Db_Table class
     *
     * @param  mixed $key The value(s) of the primary keys.
     * @return Zend_Db_Table_Rowset_Abstract Row(s) matching the criteria.
     */
	public function find($id) {
		try {
			return $this->getDbTable()->find($id);
		} catch (Exception $e) {
			return null;
		}
	}

	/**
     * Fetches rows by primary key. This utilizes the find method
     * of Zend_Db_Table class
     *
     * @param  mixed $key The value(s) of the primary keys.
     * @return array Data fetched from row(s) matching the criteria to array.
     */
	public function findToArray($id) {
		try {
			return $this->getDbTable()->find($id)->toArray();
		} catch (Exception $e) {
			return null;
		}
	}

	 /**
     * Fetches all rows.
     *
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */
	public function fetchAllObject($where = null, $order = null, $count = null, $offset = null) {
		return $this->getDbTable()->fetchAll($where, $order, $count, $offset);
	}

	 /**
     * Fetches all data as an array.
     *
     * @return array The array of results.
     */
	public function fetchAllArray($where = null, $order = null, $count = null, $offset = null) {
		return $this->fetchAllObject()->toArray($where, $order, $count, $offset);
	}

}

